<?php

namespace App\Http\Controllers\User;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use Auth,Input,Redirect;
use Log;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        //
        $id=Auth::user()->id;
        $user = User::find($id);
        return view('user.info')->with('user',$user);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        //
    }
	
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request,$id)
    {
        $this->validate($request,[            
            'email'=>'required',
            'password'=>'required',
            'password_confirmation'=>'required',
            ]);

        $user = User::find($id);

        $password =Input::get('password');
        $password_confirmation = Input::get('password_confirmation');

        $user->name =Input::get('username');
        $user->email = Input::get('email');
        $user->password = $password_confirmation;
        $user->isAdmin = false;        
		$user->hospital = Input::get('hospital');
		$user->address = Input::get('address');
        if($user->save()){
            return Redirect::back();
        }else{
            return Redirect::back()->withErrors('User save failed!Please try again.');
        }

    }
	
	/**
	 * Update current user avatar 
	 * 
	 * @param int $id
	 * @param Request
	 * 
	 * @return Response 
	 */
	public function updateAvatar(Request $request,$id)
	{
		
		$types=array(  
    	'image/jpg',  
    	'image/jpeg',  
    	'image/png',  
    	'image/pjpeg',  
    	'image/gif',  
    	'image/bmp',  
    	'image/x-png'  
		);//accepted file types  
		
		$max_file_size=2000000;//maxmium file size in bytes				
				
		if(!is_uploaded_file($_FILES['avatar']['tmp_name']))
		{
			log::warning('File does not exist.');
			exit;
		}
			
		$file = $_FILES['avatar'];
		
		if($max_file_size < $file['size'])
		{
			log::warning('File Size Exceeds Max Size Limit');
			exit;			
		}
		
		if(!in_array($file['type'], $types))
		{
			log::warning('File Type not Accepted!'.$file['type']);
			exit;
		}
		$base_dir = "D:/wamp/www/medicloud/public/users/";
		
		//create user avatar base folder
		if(!file_exists($base_dir)){
			mkdir($base_dir);	
		}		
		
		$user_dir = $base_dir.$id;
		
		//create seperate user avatar folder
		if(!file_exists($user_dir))
		{			
			log::warning('Making User Folder!');
			mkdir($user_dir);
			log::warning('Directory made');
		}
		log::info($file);
	  	$pinfo=pathinfo($file["name"]);  
    	$ftype=$pinfo['extension']; 
		$filename = $file['tmp_name'];
		$destination = $user_dir.'/avatar'.".".$ftype;
		log::info($destination);
		if(!move_uploaded_file($filename, $destination))
		{
			log::warning('Error while moving uploaded file!');
			exit;
		}
		log::info('File Accepted');
		$user = User::find($id);
		$user->avatar = '/users/'.$id.'/avatar.'.$ftype;
		if($user->save()){
			return Redirect::back()->with('user',$user);
		}
	}	
	
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        //
        $user = User::find($id);

        $user->delete();
        return Redirect::to('/');
    }
}
